package com.fortune.funcjar;

import android.annotation.SuppressLint;
import android.os.Build;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Method;
import java.util.HashMap;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public class FuncManager {
    // 内容距边框距离
    public int MARGIN_BORDER = UIUtils.dip2px(ContextUtil.getContext(), 10);
    // label与text距离
    private int HOR_SPACE = UIUtils.dip2px(ContextUtil.getContext(), 10);

    private int EDIT_PADDING_LEFT_RIGHT = UIUtils.dip2px(ContextUtil.getContext(), 10);
    private int EDIT_PADDING_TOP_BOTTOM = UIUtils.dip2px(ContextUtil.getContext(), 10);


    /**
     * 空构造，反射调用
     */
    public FuncManager() {
    }


    /**
     * 获取设备ip地址
     * @return
     */
    public String getIpAddress() {
        String ipAddress = NetUtils.getLocalIP();
        Log.e("hhhhh", "ip地址：" + ipAddress);
        return ipAddress;
    }

    /**
     * 获取设备ID
     *
     * @param
     * @return 设备id
     */

    @SuppressLint("MissingPermission")
    public String getDeviceSN() {
        String serial = "";
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {//9.0+
                serial = Build.getSerial();
            } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {//8.0+
                serial = Build.SERIAL;
            } else {//8.0-
                Class c = Class.forName("android.os.SystemProperties");
                Method get = c.getMethod("get", String.class);
                serial = (String) get.invoke(c, "ro.serialno");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("e", "读取设备序列号异常：" + e.toString());
        }
        return serial;
    }







    /**
     * 添加一行文本显示
     *
     * @param label 名称
     * @param text  内容
     */
//    public LinearLayout addTextRow(String label, String text, HashMap<String, Object> tags) {
//        LinearLayout ll_row = new LinearLayout(ContextUtil.getContext());
//        ll_row.setOrientation(LinearLayout.HORIZONTAL);
//        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
//        lp.setMargins(MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER);
//        ll_row.setLayoutParams(lp);
//
//        TextView tv_label = new TextView(ContextUtil.getContext());
//        TextView tv_text = new TextView(ContextUtil.getContext());
//        LinearLayout.LayoutParams lp_text = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
//        lp_text.setMargins(HOR_SPACE, 0, 0, 0);
//        tv_text.setLayoutParams(lp_text);
//        tv_label.setText(label + "");
//        tv_text.setText(text + "");
//        tv_text.setTag(tags);
//
//
//        ll_row.addView(tv_label);
//        ll_row.addView(tv_text);
//
//        return ll_row;
//    }


    /**
     * 添加输入框
     *
     * @param reflexCallBack 输入框回调
     * @param label          标题
     * @param text           内容
     * @param tags           附加信息
     * @return 此行线性容器
     */
    public LinearLayout addEditRow(ReflexCallBack reflexCallBack, String label, String text, HashMap<String, Object> tags) {
        LinearLayout ll_row = new LinearLayout(ContextUtil.getContext());
        ll_row.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
        lp.setMargins(MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER);
        ll_row.setLayoutParams(lp);
        ll_row.setGravity(Gravity.CENTER_VERTICAL);

        TextView tv_label = new TextView(ContextUtil.getContext());
        EditText edt_text = new EditText(ContextUtil.getContext());
        LinearLayout.LayoutParams lp_edit = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
        lp_edit.setMargins(HOR_SPACE, 0, 0, 0);
        edt_text.setLayoutParams(lp_edit);
        edt_text.setPadding(EDIT_PADDING_LEFT_RIGHT, EDIT_PADDING_TOP_BOTTOM, EDIT_PADDING_LEFT_RIGHT, EDIT_PADDING_TOP_BOTTOM);
        edt_text.setTextSize(14);
        if (!StringUtil.isEmpty(label)) {
            tv_label.setText(label);
        }
        if (!StringUtil.isEmpty(text)) {
            edt_text.setText(text);
        }
        edt_text.setWidth(MATCH_PARENT);
        edt_text.setTag(tags);
//        edt_text.setInputType(InputType.TYPE_NULL);

        boolean isNeedShow = (boolean) tags.get(Constants.TagsHashKey.IS_NEED_SHOW);



        edt_text.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        edt_text.setSingleLine(true);
        edt_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                Log.e("hhhhhh", "before---" + s + "   start:" + start + "after:" + after + "count:" + count);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                Log.e("hhhhhh", "textchange---" + s + "   start:" + start + "before " + before + "count" + count);
                // 判断输入框是否有原内容，有内容删除后赋值

                boolean isModification = false;
//                if (start != 0) {
//                    String str = s.toString();
//                    String afterStr = str.substring(start);
//                    edt_text.setText(afterStr);
//                    isModification = true;
//                }

                int type = (int)tags.get(Constants.TagsHashKey.PROPERTY_TYPE);
                // 如果类型是主动，并且大于36个字符
                if (type == 2 && s.length() > 36){
                    Log.e("hhhh", "不符合规则：长度为" + s.length());
                    edt_text.setText("");
                    return;
                }

                if (s.toString().contains("fortune-card-")){
                    edt_text.setText(s.toString().split("card-")[1]);
                    return;
                }

                // 输入框回调
                if (s.length() > 0) {
                    reflexCallBack.EditListener(s.toString(), tags, isModification);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        // 焦点置后
        edt_text.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    String strEdit = edt_text.getText().toString();
                    if (strEdit.length() > 0) {
                        edt_text.setSelection(edt_text.getText().toString().length());
                    }
                }
            }
        });

        ll_row.addView(tv_label);
        ll_row.addView(edt_text);

        if (isNeedShow){
            ll_row.setVisibility(View.VISIBLE);
        } else {
            ll_row.setVisibility(View.GONE);
        }

        boolean isNeedFocus = (boolean) tags.get(Constants.TagsHashKey.IS_NEED_FOCUS);

        if (isNeedFocus) {
            edt_text.requestFocus();
        }

        return ll_row;
    }


    /**
     * 添加按钮
     *
     * @param reflexCallBack 点击回调
     * @param text           按钮内容
     * @param tags           附加信息
     * @return 此按钮
     */
    public Button addButtonRow(ReflexCallBack reflexCallBack, String text, HashMap<String, Object> tags) {
        Button button = new Button(ContextUtil.getContext());
        button.setText(text);
        button.setTag(tags);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reflexCallBack.callBack(tags);
            }
        });
        return button;
    }



    /**
     * 请求接口
     *
     * @param url
     * @param params
     * @return
     */
    public String getWebApi(String url, HashMap<String, String> params) {
        String result = HttpConnectionUtil.getHttp().postRequest(url, params);
        Log.e("hhhhh", "请求结果：" + result);
        return result;
    }
}
